package com.xieyingwangluo.yxpart.service;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.DemoModel;
import com.xieyingwangluo.yxpart.mapper.DemoMapper;
import com.xieyingwangluo.yxpart.utils.BeanValidater;
import com.xieyingwangluo.yxpart.utils.StringBuildUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class DemoService {

    @Autowired
    private DemoMapper demoMapper;

    @Transactional(readOnly = false)
    public Result add(DemoModel demo){
        if(demo==null){
            return Result.generate(102,"参数demo对象为null",null);
        }

        Map<String,StringBuffer> err= BeanValidater.validate(demo);
        if(err!=null){
            return  Result.generate(122,"demo对象参数不合法",err);
        }

        demo.setNoid(StringBuildUtils.buildUUID());

        int cnt = demoMapper.add(demo);
        if(cnt>0) {
            return Result.generate(0, "新增demo操作成功",null);
        }else{
            return Result.generate(132, "由于未知原因，本次新增demo未能影响任何数据。",null);
        }
    }

    public Result selById(String id){

       DemoModel demo = demoMapper.getById(id);
        if(demo==null){
            return Result.generate(12,"未获得id = "+id+"的demo记录",null);
        }else {
            return Result.generate(0, "获取成功", demo);
        }
    }

    public Result selA(){
        return Result.generate(0,"获取成功",demoMapper.list());
    }

}
